#!/bin/bash

# Live555 精简版构建脚本 - 君正平台适配

echo "========================================"
echo "Live555 Minimal Build Script for Ingenic"
echo "========================================"

# 检查是否已设置交叉编译工具链
if [ -z "$CROSS_COMPILE" ]; then
    # 设置默认交叉编译工具链路径
    export CROSS_COMPILE="/home/hxg/Desktop/My_Demo/Tassadar-T32-1.0.6-20250613/software/pc/toolchain/mips-gcc540-glibc222-r3.3.7.mxu2.cve/bin/mips-linux-uclibc-gnu-"
    echo "Using default cross compiler: $CROSS_COMPILE"
else
    echo "Using cross compiler: $CROSS_COMPILE"
fi

# 检查工具链是否存在
if [ ! -f "${CROSS_COMPILE}g++" ]; then
    echo "Error: Cannot find cross compiler ${CROSS_COMPILE}g++"
    echo "Please set CROSS_COMPILE environment variable to your toolchain path"
    exit 1
fi

# 创建必要的目录
echo "Creating necessary directories..."
mkdir -p lib
mkdir -p apps/streams

# 开始编译
echo "\nStarting build process..."
echo "----------------------------------------"

# 先清理之前的编译结果
echo "Cleaning previous build..."
make clean

# 编译库和应用
echo "\nBuilding libraries and applications..."
make

# 检查编译结果
if [ $? -eq 0 ]; then
    echo "\n----------------------------------------"
    echo "Build completed successfully!"
    echo "----------------------------------------"
    echo "Executable: ./apps/simple_rtsp_server"
    echo "\nTo run the RTSP server:"
    echo "  cd apps"
    echo "  ./simple_rtsp_server"
    echo "\nPut your media files in the 'streams' directory"
else
    echo "\n----------------------------------------"
    echo "Build failed!"
    echo "----------------------------------------"
    exit 1
fi